package com.atguigu.service.impl;

import com.atguigu.dao.MainTaskDao;
import com.atguigu.dao.impl.MainTaskDaoImpl;
import com.atguigu.pojo.MainTask;
import com.atguigu.pojo.Page;
import com.atguigu.service.MainTaskService;

import java.util.List;

public class MainTaskServiceImpl implements MainTaskService {
    MainTaskDao mainTaskDao=new MainTaskDaoImpl();
    @Override
    public void addMainTask(MainTask mainTask) {
        mainTaskDao.addMainTask(mainTask);
    }

    @Override
    public void updateMainTask(MainTask mainTask) {
        mainTaskDao.updateMainTask(mainTask);
    }

    @Override
    public void deleteMainTaskById(String id) {
        mainTaskDao.deleteMainTaskById(id);
    }

    @Override
    public MainTask queryMainTaskById(String id) {
        return mainTaskDao.queryMainTaskById(id);
    }
    @Override
    public MainTask queryMainTaskByLastId(String LastId) {
        return mainTaskDao.queryMainTaskByLastId(LastId);
    }

    @Override
    public List<MainTask> queryMainTasks() {
        return mainTaskDao.queryMainTasks();
    }

    @Override
    public Page page(Integer pageNo, Integer pageSize) {
        Page<MainTask> page=new Page<>();
        page.setPagesize(pageSize);
        Integer pageTotalCount=mainTaskDao.queryForPageTotalCount();
        page.setPageTotalCount(pageTotalCount);
        Integer pageTotal=pageTotalCount/pageSize;
        if(pageTotalCount % pageSize >0){
            pageTotal++;//总条数,除以一页条数,大于0,页码数+1
        }
        page.setPageTotal(pageTotal);
        System.out.println("pageNo 原来是 is:"+pageNo);
        if(pageNo<1){//防止用户输入量太大
            pageNo=1;
        }else if(pageNo>pageTotal){
            pageNo=pageTotal;
        }
        page.setPageNo(pageNo);//当前页码
        System.out.println("pageNo 现在是 is:"+pageNo);
        System.out.println("pageTotal is:"+pageTotal);
        //要得到的是page的items
        Integer begin=(page.getPageNo()-1)*pageSize;
        List<MainTask> items=mainTaskDao.queryForPageItems(begin,pageSize);
        page.setItems(items);
        return page;
    }
}
